package com.app.business.service;

import com.app.business.model.AzProject;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.jpa.PageBean;
import com.app.system.jpa.Wrapper;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.WebUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安置项目管理
 * Created by ddz on 2019/2/11.
 */
@Service
public class AzProjectService extends JpaBaseDao<AzProject, Integer> {
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private UserInfoForPropertyService userInfoForPropertyService;

    /**
     * 得到安置项目分页列表
     *
     * @return
     */
    public PageBean<AzProject> getAzProjectPageBean() {
        //安置项目集合
        Map<String, DataDict> townMap = dataDictService.getMapByParentNameForName("乡镇");

        UserInfo userInfo = WebUtils.getLoginUserInfo();
        String town = null;
        String townStr = null;
        String townCode = null;
        if (userInfo.getDepartment().getCode() != null && !userInfo.getDepartment().getCode().equals("001")) {
            town = userInfo.getDepartment().getName();
            townStr = town.substring(0, 2) + "片区";
            townCode = townMap.get(townStr).getValue();
        }

        //查询语句
        StringBuffer query = new StringBuffer("distinct azp from AzProject azp ");

        //默认排序(PageBean指定排序字段类型的话，则默认排序不使用)
        StringBuffer orderBy = new StringBuffer("azp.num,azp.projectCode,azp.id");
        Wrapper wrapper = new Wrapper(query, orderBy);
        if (townCode != null) {
            wrapper.andEq("azp.town", townCode);
        }
        wrapper.initSearchParams();
        return this.pageHql(wrapper);
    }


    /**
     * 得到安置项目分页列表
     *
     * @return
     */
    public PageBean<AzProject> getPropertyAzProjectPageBean() {
        //安置项目集合
        UserInfo userInfo = WebUtils.getLoginUserInfo();
        List<Integer> azProjectIdList = this.userInfoForPropertyService.findAzProjectIdByUserIdAndTowm(userInfo.getId());

        //查询语句
        StringBuffer query = new StringBuffer("distinct azp from AzProject azp ");
        //默认排序(PageBean指定排序字段类型的话，则默认排序不使用)
        StringBuffer orderBy = new StringBuffer("azp.num,azp.projectCode,azp.id");
        Wrapper wrapper = new Wrapper(query, orderBy);

        wrapper.initSearchParams();
        wrapper.andIn("azp.id", azProjectIdList);
        return this.pageHql(wrapper);
    }

    /**
     * 将所有的安置项目封装成map
     *
     * @return
     */
    public Map<String, AzProject> getAzProjectMap() {
        Map<String, AzProject> resultMap = Maps.newHashMap();
        List<AzProject> azProjectList = this.azProjectList();
        if (CollectionUtils.isNotEmpty(azProjectList)) {
            for (AzProject azProject : azProjectList) {
                resultMap.put(azProject.getId().toString(), azProject);
            }
        }
        return resultMap;
    }

    /**
     * 得到安置项目列表
     *
     * @return
     */
    public List<AzProject> azProjectList() {
        //查询语句
        StringBuffer hql = new StringBuffer("from AzProject azp order by azp.num,azp.projectCode,azp.id");
        return this.findHql(hql.toString(), null);
    }

    /**
     * 根据乡镇得到安置项目列表
     *
     * @return
     */
    public List<AzProject> azProjectByTownList(String town) {
        //查询语句
        StringBuffer hql = new StringBuffer("from AzProject azp ");
        if (town != null && !town.equals("")) {
            hql.append(" where azp.town = :town");
        }
        hql.append(" order by azp.num,azp.projectCode,azp.id");
        Map<String, Object> map = new HashMap<>();
        if (town != null && !town.equals("")) {
            map.put("town", town);
        }
        return this.findHql(hql.toString(), map);
    }

    /**
     * 根据乡镇得到安置项目列表
     *
     * @return
     */
    public List<AzProject> azProjectByTownAndChoosedList(String town) {
        List<Integer> azProjectIdList = this.houseService.getAzprojectIdListByTown(town);
        String ids = "0";
        if (CollectionUtils.isNotEmpty(azProjectIdList)) {
            for (Integer integer : azProjectIdList) {
                ids = ids + "," + integer.toString();
            }
        }
        //查询语句
        StringBuffer hql = new StringBuffer("from AzProject azp ");
        hql.append(" where 1 = 1 ");
        if (town != null && !town.equals("")) {
            hql.append(" and azp.id in (" + ids + ")");
        }
        hql.append(" and azp.id in (select h.azProject.id from House h where h.houseStatus = 2 )");
        hql.append(" order by azp.num,azp.projectCode,azp.id");
        Map<String, Object> map = new HashMap<>();
        return this.findHql(hql.toString(), map);
    }


    /**
     * 通过AzpProjectIdList得到安置项目列表
     *
     * @return
     */
    public List<AzProject> azProjectListByAzProjectIdList(List<String> azProjectIdList) {
        //查询语句
        String hql = " from AzProject azp where azp.id in:azProjectIdList";
        Map<String, Object> map = new HashMap<>();
        map.put("azProjectIdList", azProjectIdList);
        return this.findHql(hql, map);
    }

    /**
     * 安置项目编号查重验证
     *
     * @param azProjectCode,AzProjectId
     * @return
     */
    public List<AzProject> getAzProjectListByAzProjectCode(String azProjectCode, Integer azProjectId) {
        StringBuffer hql = new StringBuffer(" from AzProject a where a.projectCode =:azProjectCode and a.id!=:azProjectId");
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("azProjectCode", azProjectCode);
        map.put("azProjectId", azProjectId);
        return this.findHql(hql.toString(), map);
    }

    /**
     * 得到某些征收项目下对应的安置项目
     *
     * @param zsProjectIdStr
     * @return
     */
    public List<AzProject> getAzProjectListByZsProjectIdStr(String zsProjectIdStr) {
        if (zsProjectIdStr.equals("")) {
            return null;
        }
        StringBuffer hql = new StringBuffer("from AzProject ap where ap.id in (");
        hql.append("SELECT DISTINCT h.azProject.id FROM ZsProjectHouse zp,House h WHERE zp.houseId = h.id AND zp.zsProjectId IN (" + zsProjectIdStr + ")");
        hql.append(") ORDER BY ap.id");
        return this.findHql(hql.toString(), null);
    }

    /**
     * 得到某些征收项目下对应的安置项目
     *
     * @param azProjectId
     * @return
     */
    public List<AzProject> getAzProjectListByAzProjectId(String azProjectId) {

        StringBuffer hql = new StringBuffer("from AzProject ap where ap.id in (");
        hql.append(" " + azProjectId + " ");
//        hql.append("SELECT DISTINCT h.azProject.id FROM ZsProjectHouse zp,House h WHERE zp.houseId = h.id AND zp.zsProjectId IN (" + zsProjectIdStr + ")");
        hql.append(") ORDER BY ap.id");
        return this.findHql(hql.toString(), null);
    }

    /**
     * 获取某些安置项目
     *
     * @param azProjectIdStr
     * @return
     */
    public List<AzProject> getAzProjectListByAzProjectIdStr(String azProjectIdStr) {
        StringBuffer hql = new StringBuffer("from AzProject ap where ap.id in (" + azProjectIdStr + ") order by ap.id");
        return this.findHql(hql.toString(), null);
    }


    @Transactional
    public List<String> getTownList() {
        String sql = "select DISTINCT town from az_project order by num ";
        return this.entityManager.createNativeQuery(sql).getResultList();
    }

    @Transactional
    public List<String> getTownList(String town) {
        String sql = "select DISTINCT town from az_project WHERE town =:town order by num ";
        return this.entityManager.createNativeQuery(sql).setParameter("town", town).getResultList();
    }

    @Transactional
    public List<Integer> getAzProjectIdList(String town) {
        String sql = "SELECT DISTINCT id FROM az_project WHERE town =:town ORDER BY num  ";
        return this.entityManager.createNativeQuery(sql).setParameter("town", town).getResultList();
    }

}
